﻿using AuthCenter.Domain;
using AuthCenter.ErorCode;
using AuthCenter.Infrastructure;
using AuthCenter_API.Read.Applications.Commands.User;
using AuthCenter_API.Read.Dtos;
using MediatR;

namespace AuthCenter_API.Read.Applications.CommandHandlers.User
{
    public class UserQueryCommandHandler : IRequestHandler<UserQueryCommand, APIResult<APIPaging<UserDto>>>
    {
        private readonly IBaseRepository<ShengModel> _shengRepository;
        private readonly IBaseRepository<ShiModel> _shiRepository;
        private readonly IBaseRepository<XianModel> _xianRepository;
        private readonly IBaseRepository<UserModel> _userRepository;
        private readonly IBaseRepository<UserRoleModel> _urRepository;
        private readonly IBaseRepository<RoleModel> _roleRepository;

        public UserQueryCommandHandler(IBaseRepository<ShengModel> shengRepository, IBaseRepository<ShiModel> shiRepository, IBaseRepository<XianModel> xianRepository, IBaseRepository<UserModel> userRepository, IBaseRepository<UserRoleModel> urRepository, IBaseRepository<RoleModel> roleRepository)
        {
            _shengRepository = shengRepository;
            _shiRepository = shiRepository;
            _xianRepository = xianRepository;
            _userRepository = userRepository;
            _urRepository = urRepository;
            _roleRepository = roleRepository;
        }

        public Task<APIResult<APIPaging<UserDto>>> Handle(UserQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<UserDto>> result = new APIResult<APIPaging<UserDto>>();
            result.Code = APIEnum.QuerySuccess;
            result.Message = APIEnum.QuerySuccess.Desc();
            var m = (from a in _userRepository.GetAll()
                     join b in _shengRepository.GetAll()
                     on a.ShengId equals b.ShengId
                     join c in _shiRepository.GetAll()
                     on a.ShiId equals c.ShiId
                     join d in _xianRepository.GetAll()
                     on a.XianId equals d.XianId
                     //join e in _urRepository.GetAll()
                     //on a.Id equals e.UserId
                     //join f in _roleRepository.GetAll()
                     //on e.RoleId equals f.RoleId
                     where a.IsDel == 0
                     select new UserDto
                     {
                         Id = a.Id,
                         UserName = a.UserName,
                         Password = a.Password,
                         UserSex = a.UserSex,
                         ShengId = a.ShengId,
                         ShiId = a.ShiId,
                         XianId = a.XianId,
                         AddrDesc = a.AddrDesc,
                         CreateName = a.CreateName,
                         CreateTime = a.CreateTime,
                         UpdateName = a.UpdateName,
                         UpdateTime = a.UpdateTime,
                         ShengName = b.ShengName,
                         ShiName = c.ShiName,
                         XianName = d.XianName,
                         DepId = a.DepId,
                         //URId = e.URId,
                         //UserId = e.UserId,
                         //RoleId = e.RoleId,
                         RoleName = ""
                     }).ToList();
            foreach (var b in m)
            {
                var ridlist = _urRepository.GetAll().Where(p => p.UserId == b.Id).Select(x => x.RoleId);
                var rolelist = _roleRepository.GetAll().Where(p => ridlist.Contains(p.RoleId)).Select(x => x.RoleName);
                b.RoleName = string.Join("、", rolelist);
            }
            var count = m.Count();
            var page = (int)Math.Ceiling(1.0 * count / request.Size);
            m = m.OrderByDescending(x => x.Id).Skip((request.Index - 1) * request.Size).Take(request.Size).ToList();
            APIPaging<UserDto> p = new APIPaging<UserDto>();
            p.TotalPage = page;
            p.TotalCount = count;
            p.List = m;
            result.Data = p;
            return Task.FromResult(result);
        }
    }
}
